const mysql = require("mysql");

//1.创建连接池对象
const pool = mysql.createPool({
    host: "127.0.0.1",
    user: "root",
    password: "root",
    database: "guomei",
    port: 3306,
    timeout: 1000 * 5
})

function execute(sql, params) {
    return new Promise(function (resolve, reject) {

        //2.从连接池中取出连接对象
        pool.getConnection(function (err, conn) {
            if (err) {
                console.log("连接数据库失败!", err.message);
                reject(err.message);
            }
            //3.执行sql语句
            conn.query(sql, params, function (err, result) {
                if (err) {
                    console.log("执行sql语句失败!", err.message);
                    reject(err.message);
                }
                resolve(result);
                //4.返回结果,释放连接
                conn.release();
            })

        })
    })
}

module.exports = {
    execute
}


